package org.jeecg.modules.fk.service;

import org.jeecg.modules.fk.mapper.auto.model.RiskIndicatorDefineWithBLOBs;
import org.jeecg.modules.fk.mapper.auto.model.RiskIndicatorStatistic;
import org.jeecg.modules.fk.mapper.auto.model.RiskIndicatorTemplateWithBLOBs;
import org.jeecg.modules.fk.vo.indicator.IndicatorDefineVO;
import org.jeecg.modules.fk.vo.indicator.IndicatorStatisticVO;
import org.jeecg.modules.fk.vo.indicator.IndicatorTemplateVO;
import org.jeecg.modules.fk.constant.ZbTypeEnum;
import org.jeecg.modules.fk.repository.FkDaoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 风控指标 相关
 */
@Slf4j
@Service
public class RiskIndicatorService {
    @Autowired
    private FkDaoRepository daoRepository;

    /**
     * 获取 风险指标统计 集合
     *
     * @param zbType
     * @return
     */
    public List<IndicatorStatisticVO> getStatisticList(ZbTypeEnum zbType) {
        List<RiskIndicatorStatistic> zbtjList = this.daoRepository.getRiskIndicatorExtMapper().getIndicatorDefineByListYwdlDm(zbType.toString());
        return zbtjList.stream()
                .map(e -> {
                    IndicatorStatisticVO indicatorStatisticVO = new IndicatorStatisticVO();
                    BeanUtils.copyProperties(e, indicatorStatisticVO);
                    return indicatorStatisticVO;
                })
                .collect(Collectors.toList());
    }

    /**
     * 获取风控指标类型定义
     *
     * @param zbType 指标类型
     */
    public List<IndicatorStatisticVO> getDefineGroup(ZbTypeEnum zbType) {
        //风险指标统计
        List<IndicatorStatisticVO> indicatorStatisticList = this.getStatisticList(zbType);
        if (indicatorStatisticList == null || indicatorStatisticList.isEmpty()) {
            return new ArrayList();
        }

        //指标定义
        List<RiskIndicatorDefineWithBLOBs> zbdyList = this.daoRepository.getRiskIndicatorExtMapper().getIndicatorDefineListByYwdlDm(zbType.toString());
        if (zbdyList == null || zbdyList.isEmpty()) {
            return indicatorStatisticList;
        }
        //按 业务小类代码 分组
        Map<String, List<IndicatorDefineVO>> zbdyMap = zbdyList.stream()
                .map(e -> {
                    IndicatorDefineVO indicatorDefineVO = new IndicatorDefineVO();
                    BeanUtils.copyProperties(e, indicatorDefineVO);
                    return indicatorDefineVO;
                })
                .collect(Collectors.groupingBy(IndicatorDefineVO::getYwxlDm));

        //组装最后数据
        for (IndicatorStatisticVO statisticVO : indicatorStatisticList) {
            statisticVO.setDefineList(zbdyMap.get(statisticVO.getYwxlDm()));
        }
        return indicatorStatisticList;
    }

    /**
     * 获取风控指标类型定义 map
     *
     * @param zbType 风控类型
     */
    public Map<String, IndicatorDefineVO> getDefineMap(ZbTypeEnum zbType) {
        //指标定义
        List<RiskIndicatorDefineWithBLOBs> zbdyList = this.daoRepository.getRiskIndicatorExtMapper().getIndicatorDefineListByYwdlDm(zbType.toString());
        if (zbdyList == null || zbdyList.isEmpty()) {
            return new HashMap<>();
        }

        return zbdyList.stream()
                .map(e -> {
                    IndicatorDefineVO indicatorDefineVO = new IndicatorDefineVO();
                    BeanUtils.copyProperties(e, indicatorDefineVO);
                    return indicatorDefineVO;
                })
                .collect(Collectors.toMap(IndicatorDefineVO::getZbbh, e -> e));
    }

    /**
     * 获取风控指标模板 map
     *
     * @param zbType 风控类型
     * @Result &lt;业务描述编号, IndicatorTemplateVO&gt;
     */
    public Map<String, IndicatorTemplateVO> getTemplateMap(ZbTypeEnum zbType) {
        //指标模板
        List<RiskIndicatorTemplateWithBLOBs> zbmbList = this.daoRepository.getRiskIndicatorExtMapper().getIndicatorTemplateListByZbbhPrefix(zbType == null ? null : zbType.toString());
        if (zbmbList == null || zbmbList.isEmpty()) {
            return new HashMap<>();
        }

        return zbmbList.stream()
                .map(e -> {
                    IndicatorTemplateVO indicatorTemplateVO = new IndicatorTemplateVO();
                    BeanUtils.copyProperties(e, indicatorTemplateVO);
                    return indicatorTemplateVO;
                })
                .collect(Collectors.toMap(IndicatorTemplateVO::getYwmsbh, e -> e));
    }

    /**
     * 获取风控指标模板 map
     *
     * @param zbType 风控类型
     * @Result &lt;业务描述编号, IndicatorTemplateVO&gt;
     */
    public Map<String, IndicatorTemplateVO> getTemplateStrMap(ZbTypeEnum zbType) {
        Map<String, IndicatorTemplateVO> templateMap = this.getTemplateMap(zbType);
        if (templateMap.isEmpty()) {
            return new HashMap<>();
        }
        return templateMap;


//        Map<String, String> templateStrMap = new HashMap<>(templateMap.size());
//        templateMap.entrySet().forEach(e -> {
//            templateStrMap.put(e.getKey(), e.getValue().getYwmsMb());
//        });
//        return templateStrMap;
    }

    /**
     * 获取风控指标类型定义
     *
     * @param zbbhPrefix 指标编号前缀
     */
    public Map<String, List<IndicatorDefineVO>> getDefineGroup2(String zbbhPrefix) {
        //风险指标统计
//        List<IndicatorStatisticVO> indicatorStatisticList = this.getStatisticList(zbType);
//        if (indicatorStatisticList == null || indicatorStatisticList.isEmpty()) {
//            return new ArrayList();
//        }


        //指标定义
        List<RiskIndicatorDefineWithBLOBs> zbdyList = this.daoRepository.getRiskIndicatorExtMapper().getIndicatorDefineListByZbbhPrefix(zbbhPrefix);
        if (zbdyList == null || zbdyList.isEmpty()) {
            return Collections.EMPTY_MAP;
        }

        //按 业务小类代码 分组
        Map<String, List<IndicatorDefineVO>> zbdyMap = zbdyList.stream()
                .map(e -> {
                    IndicatorDefineVO indicatorDefineVO = new IndicatorDefineVO();
                    BeanUtils.copyProperties(e, indicatorDefineVO);
                    return indicatorDefineVO;
                })
                .collect(Collectors.groupingBy(IndicatorDefineVO::getYwxlDm));

//        //组装最后数据
//        for (IndicatorStatisticVO statisticVO : indicatorStatisticList) {
//            statisticVO.setDefineList(zbdyMap.get(statisticVO.getYwxlDm()));
//        }
        return zbdyMap;
    }


}
